﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
public partial class AdminPortal_Controls_SanPham_ProductCategory_List : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    TblDanhMucSanPhamManager _TblDanhMucSanPhamManager = new TblDanhMucSanPhamManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadToGridvew();

            LoadDanhMucCha();
        }
    }

    private void LoadToGridvew()
    {
        TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity = new TblDanhMucSanPhamEntity();
        _TblDanhMucSanPhamEntity.LangId = LangID;

        DataTable _dt = _TblDanhMucSanPhamManager.be_SelectToGrivew(_TblDanhMucSanPhamEntity);

        if (_dt != null)
        {
            lblTotalRecord.Text = _dt.Rows.Count.ToString();
            dgvListCategoryNews.DataSource = _dt;
            dgvListCategoryNews.DataBind();
        }
    }

    private void LoadDataToGrid(TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity)
    {
        DataTable dt = _TblDanhMucSanPhamManager.be_SelectToDataList(_TblDanhMucSanPhamEntity);
        if (dt != null)
        {
            lblTotalRecord.Text = dt.Rows.Count.ToString();
            dgvListCategoryNews.DataSource = dt;
            dgvListCategoryNews.DataBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/ProductCategoryAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvListCategoryNews.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            if (chkID.Checked)
            {
                try
                {
                    //set lai DanhMuctin ID cua tin trong danh muc nay
                    //CapNhatLaiDanhMucIDKhiXoaDanhMUc(Convert.ToInt32(chkID.Value));
                    _TblDanhMucSanPhamManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
        LoadDataToGrid(new TblDanhMucSanPhamEntity());
    }

    private void CapNhatLaiDanhMucIDKhiXoaDanhMUc(int DanhMucID)
    {
        int DanhMucRootID = 0;
        DataTable _dtSanPham = new TblSanPhamManager().be_selectToDropdownlist(DanhMucID);
        if (_dtSanPham != null)
        {
            DataTable _dtDanhMucSanPhamRoot = _TblDanhMucSanPhamManager.SelectByDanhMucChaRDT(0);
            if (_dtDanhMucSanPhamRoot.Rows.Count > 0)
                DanhMucRootID = Convert.ToInt32(_dtDanhMucSanPhamRoot.Rows[0]["ID"].ToString());
            foreach (DataRow r in _dtSanPham.Rows)
            {
                TblSanPhamEntity _TblSanPhamEntity = new TblSanPhamManager().SelectOne(Convert.ToInt32(r[TblSanPhamFields.Id.Name].ToString()));
                _TblSanPhamEntity.DanhMucSpid = DanhMucRootID;
                new TblSanPhamManager().Update(_TblSanPhamEntity);                
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity = new TblDanhMucSanPhamEntity();
        _TblDanhMucSanPhamEntity.SortExpression = TblDanhMucSanPhamFields.ThuTu.Name;
        _TblDanhMucSanPhamEntity.SortDirection = "ASC";
        _TblDanhMucSanPhamEntity.Condition = txtKeySearch.Text;
        _TblDanhMucSanPhamEntity.LangId = LangID;

        LoadDataToGrid(_TblDanhMucSanPhamEntity);

    }
    protected void dgvListCategoryNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkCategoryName = (HyperLink)e.Row.FindControl("lnkCategoryName");

        Image imgUpdate = (Image)e.Row.FindControl("imgUpdate");

        Image imgIcon = (Image)e.Row.FindControl("imgIcon");

        if (r != null && lnkCategoryName != null && imgUpdate != null)
        {
            lnkCategoryName.Text = r[TblDanhMucSanPhamFields.TenDanhMuc.Name].ToString();
            lnkCategoryName.NavigateUrl = "~/AdminPortal/Pages/ProductCategoryEdit.aspx?id=" + r[TblDanhMucSanPhamFields.Id.Name].ToString();

            if (r[TblDanhMucSanPhamFields.TrangThai.Name].ToString().Equals("True"))
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_yes.png";
            else
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_no.png";
            imgUpdate.Attributes.Add("onclick", "javascript:changeStatuscateProduct('" + imgUpdate.ClientID + "','" + r[TblDanhMucSanPhamFields.Id.Name].ToString() + "')");

            if (!string.IsNullOrEmpty(r[TblDanhMucSanPhamFields.Icon.Name].ToString()))
                imgIcon.ImageUrl = r[TblDanhMucSanPhamFields.Icon.Name].ToString();
            else
                imgIcon.Visible = false;
        }
    }

    protected void ddlDanhMucCha_SelectedIndexChanged(object sender, EventArgs e)
    {
        TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity = new TblDanhMucSanPhamEntity();
        _TblDanhMucSanPhamEntity.SortDirection = "ASC";
        string txt = ddlDanhMucCha.SelectedItem.Text;
        txt = txt.Replace("--", "");
        _TblDanhMucSanPhamEntity.Condition = txt;
        _TblDanhMucSanPhamEntity.LangId = LangID;

        LoadDataToGrid(_TblDanhMucSanPhamEntity);
    }

    private void LoadDanhMucCha()
    {
        TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity = new TblDanhMucSanPhamEntity();
        _TblDanhMucSanPhamEntity.LangId = LangID;
        DataTable _dtCha = _TblDanhMucSanPhamManager.be_SelectToGrivew(_TblDanhMucSanPhamEntity);        
        if (_dtCha != null)
        {
            //HNG.LIB.BIND_SOURCE.Bind_DropDownListV3(ddlDanhMucCha, _dtCha, TblDanhMucSanPhamFields.TenDanhMuc.Name, TblDanhMucSanPhamFields.Id.Name, TblDanhMucSanPhamFields.DanhMucCha.Name);
            ddlDanhMucCha.DataTextField = TblDanhMucSanPhamFields.TenDanhMuc.Name;
            ddlDanhMucCha.DataValueField = TblDanhMucSanPhamFields.Id.Name;
            ddlDanhMucCha.DataSource = _dtCha;
            ddlDanhMucCha.DataBind();
        }
    }
}